package oleg.log; 

/**
 * Copyright 2008,2009 Oleg Frenkel
 *
 * This file is part of PerfectSearch.
 *
 * PerfectSearch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PerfectSearch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PerfectSearch.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.pim.*;

import oleg.cfg.Config;
import oleg.locale.LocaleManager;
import oleg.rms.RMSManager;
import oleg.C;
import oleg.MainSearch;

public class LogView
{
  private MainSearch mainSearch;

  private Form logForm = new Form("Log");
  private Display display;
  private Displayable prevScreen;

  private Command backCommand = new Command("Back", Command.BACK, 2);

  public LogView(MainSearch mainSearch) {
    this.mainSearch = mainSearch;
    display = Display.getDisplay(mainSearch);

//     updateInterface();

    initForm();
  }

  public void updateInterface() {
//     phonesList.removeCommand(exitCommand);
    logForm.removeCommand(backCommand);

    LocaleManager localeMgr = LocaleManager.instance();

    backCommand = new Command(localeMgr.getProperty(C.BACK), Command.BACK, 2);
    logForm.addCommand(backCommand);
  }

  private void initForm() {
    logForm.addCommand(backCommand);
    logForm.setCommandListener(new ViewCommandListener());
  }

  private void fillFormFromLog() {
    logForm.deleteAll();
    Vector logs = Logger.getLog();
    for (int i = logs.size() - 1; i >= 0; i--) {
      logForm.append("*  " + (String)logs.elementAt(i) + "\n");
    }
  }

  public void showView() {
    fillFormFromLog();
    prevScreen = display.getCurrent();
    display.setCurrent(logForm);
  }


  private class ViewCommandListener implements CommandListener
  {
    public void commandAction(Command c, Displayable s) {
//       if (c == exitCommand) {
//         mainSearch.exitApplication();
//         return;
//       }

      if (c == backCommand) {
        display.setCurrent(prevScreen);
        return;
      }

    }
  }

}
